{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gc\n",
    "import os\n",
    "import pandas as pd\n",
    "from utils.misc import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The dataset can be downloaded from https://labs.criteo.com/2014/02/download-kaggle-display-advertising-challenge-dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATA_DIR = os.path.abspath('../../Data/display_advertising_challenge')\n",
    "old_data_dir = os.path.join(DATA_DIR, 'dac')\n",
    "new_data_dir = os.path.join(DATA_DIR, 'processed')\n",
    "\n",
    "if not os.path.exists(new_data_dir):\n",
    "    os.makedirs(new_data_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "column_names = pd.Index(['label'] + ['I%d' % (i + 1) for i in range(13)] + ['C%d' % (i + 1) for i in range(26)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Elapsed time: 1357 sec\n"
     ]
    }
   ],
   "source": [
    "with get_elapsed_time():\n",
    "    df_train = pd.read_csv(os.path.join(old_data_dir, 'train.txt'), sep='\\t', names=column_names)\n",
    "    df_train.to_pickle(os.path.join(new_data_dir, 'df_train+valid+test.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# of obs in train set: 45840617\n"
     ]
    }
   ],
   "source": [
    "print('# of obs in train set:', df_train.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_size = 0.2\n",
    "split_index = round(df_train.shape[0] * test_size)\n",
    "df_train, df_test = df_train[:-split_index], df_train[-split_index:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Elapsed time: 711 sec\n"
     ]
    }
   ],
   "source": [
    "with get_elapsed_time():\n",
    "    df_train.to_pickle(os.path.join(new_data_dir, 'df_train+valid.pkl'))\n",
    "    df_test.to_pickle(os.path.join(new_data_dir, 'df_test.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# of obs in train set: 36672494 \n",
      "# of obs in test set: 9168123\n"
     ]
    }
   ],
   "source": [
    "print('# of obs in train set:', df_train.shape[0], '\\n# of obs in test set:', df_test.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "valid_size = 0.2\n",
    "split_index = round(df_train.shape[0] * valid_size)\n",
    "df_train, df_valid = df_train[:-split_index], df_train[-split_index:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Elapsed time: 507 sec\n"
     ]
    }
   ],
   "source": [
    "with get_elapsed_time():\n",
    "    df_train.to_pickle(os.path.join(new_data_dir, 'df_train.pkl'))\n",
    "    df_valid.to_pickle(os.path.join(new_data_dir, 'df_valid.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# of obs in train set: 29337995 \n",
      "# of obs in valid set: 7334499\n"
     ]
    }
   ],
   "source": [
    "print('# of obs in train set:', df_train.shape[0], '\\n# of obs in valid set:', df_valid.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "del df_train, df_valid, df_test\n",
    "_ = gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Elapsed time: 53 sec\n"
     ]
    }
   ],
   "source": [
    "with get_elapsed_time():\n",
    "    df_quiz = pd.read_csv(os.path.join(old_data_dir, 'test.txt'), sep='\\t', names=column_names[1:])\n",
    "    df_quiz.to_pickle(os.path.join(new_data_dir, 'df_quiz.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# of obs in quiz set: 6042135\n"
     ]
    }
   ],
   "source": [
    "print('# of obs in quiz set:', df_quiz.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "del df_quiz\n",
    "_ = gc.collect()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
